/////////////////////////////////////////////
//	File			:	"WinMain.cpp"
/////////////////////////////////////////////

#include <windows.h>
#include "Game.h"

//Use to verify And creation of window
const char* szClassName				= "BoxTopProject";			
const char* szGameWindowName		= "Final Project Game";	
const int	unWindowWidth			= 1024;					
const int	unWindowHeight			= 720;						

#ifdef _DEBUG
	const bool	bIsWindowed			= true;						
#else
	const bool	bIsWindowed			= false;
#endif

//Functions
bool CheckIfAlreadyRunning(void);
LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
BOOL RegisterWindow(HINSTANCE hInstance);
HWND MakeWindow(HINSTANCE hInstance);


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	MSG		msg;	
	HWND	hWnd;	

	//Check for one Instance
	if (!hPrevInstance)
	{
		if (CheckIfAlreadyRunning())
			return FALSE;
	}

	//	Register
	if (!RegisterWindow(hInstance))
		return 0;

	//	Create the window
	hWnd = MakeWindow(hInstance);

	if (!hWnd)
		return 0;

	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

	//	Initialize Game	
	CGame* pGame = CGame::GetInstance();
	pGame->Initialize(hWnd,hInstance,unWindowWidth,unWindowHeight,bIsWindowed);

	///////////////////////////////////////////////////

	//	Enter main event loop
	while (TRUE)
	{
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{ 
			if (msg.message == WM_QUIT)
				break;
		
			TranslateMessage(&msg);

			DispatchMessage(&msg);
		}
		
		//	Put Game Logic Here
		if(pGame->Main() ==	false)
			PostQuitMessage(0);
		////////////////////////////////////////////////
	}

	//	Shutdown Game Here
	pGame->Shutdown();
	////////////////////////////////////////////////////
	
	UnregisterClass(szClassName, hInstance);

	return (int)(msg.wParam);
}

bool CheckIfAlreadyRunning(void)
{
	HWND hWnd = FindWindow(szClassName, szGameWindowName);

	if (hWnd)
	{
		if (IsIconic(hWnd))
			ShowWindow(hWnd, SW_RESTORE);

		SetForegroundWindow(hWnd);

		return TRUE;
	}

	return FALSE;
}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{

	PAINTSTRUCT	ps;			
	HDC			hdc;		


	switch(msg)
	{

		case WM_SYSKEYUP:
		case WM_SYSCHAR:
			return(0);
		break;
		
		case WM_CLOSE:
		{
			DestroyWindow(hWnd);			
		}
		break;

		case WM_ACTIVATE:
		{
			//	Pause
			//	gaining focus
			if (LOWORD(wParam) != WA_INACTIVE)
			{
				// unpause game code here
			}
			else // losing focus
			{
				// pause game code here

				//if (CGame::GetInstance()->GetCurrentState() == CGamePlayState::GetInstance())
				//	CGame::GetInstance()->Push(CPauseMenuState::GetInstance());
			}
			///////////////////////////////////////////
		}
		break;

		case WM_CREATE: 
		{
			//	Game initialization 
			///////////////////////////////////////////
			return(0);
		}
		break;

		case WM_PAINT:
		{
			
			hdc = BeginPaint(hWnd,&ps);

			EndPaint(hWnd,&ps);
			return(0);
		}
		break;

		case WM_DESTROY: 
		{			
			PostQuitMessage(0);
			return(0);
		}
		break;

		default:
		break;
	}

	return (DefWindowProc(hWnd, msg, wParam, lParam));
}


BOOL RegisterWindow(HINSTANCE hInstance)
{
	WNDCLASSEX	winClassEx;	

	//	Fill Out Everything
	winClassEx.cbSize			= sizeof(winClassEx);
	winClassEx.style			= CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
	winClassEx.lpfnWndProc		= WindowProc;
	winClassEx.cbClsExtra		= 0;
	winClassEx.cbWndExtra		= 0;
	winClassEx.hInstance		= hInstance;
	winClassEx.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
	winClassEx.hIconSm			= NULL;
	winClassEx.hCursor			= LoadCursor(NULL, IDC_ARROW);
	winClassEx.hbrBackground	= (HBRUSH)GetStockObject(BLACK_BRUSH);
	winClassEx.lpszMenuName		= NULL; 
	winClassEx.lpszClassName	= szClassName;

	//	Register the window class
	return RegisterClassEx(&winClassEx);
}


HWND MakeWindow(HINSTANCE hInstance)
{
	// Setup window style flags
	DWORD dwWindowStyleFlags = WS_VISIBLE;

	if (bIsWindowed)
	{
		dwWindowStyleFlags |= WS_OVERLAPPEDWINDOW;
	}
	else
	{
		dwWindowStyleFlags |= WS_POPUP;
	}

	RECT rWindow;
	rWindow.left	= 0;
	rWindow.top		= 0;
	rWindow.right	= unWindowWidth;
	rWindow.bottom	= unWindowHeight;


	AdjustWindowRectEx(&rWindow, dwWindowStyleFlags, FALSE, WS_EX_APPWINDOW);


	int nWindowWidth	= rWindow.right - rWindow.left;
	int nWindowHeight	= rWindow.bottom - rWindow.top;

	//	Create the window
	return CreateWindowEx(WS_EX_APPWINDOW, szClassName,	szGameWindowName,dwWindowStyleFlags,
						  (GetSystemMetrics(SM_CXSCREEN)/2) - (nWindowWidth/2),	(GetSystemMetrics(SM_CYSCREEN)/2) - (nWindowHeight/2),	
						  nWindowWidth, nWindowHeight, NULL,NULL,hInstance,NULL);

}